home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / installboot / RCS / installboot.c,v < prev    next >
Encoding:
Text File  |  1992-01-09  |  13.2 KB  |  594 lines

  1. head     1.8;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.8
  10. date     92.01.08.22.54.48;  author jhh;  state Exp;
  11. branches ;
  12. next     1.7;
  13.  
  14. 1.7
  15. date     92.01.08.22.49.27;  author dlong;  state Exp;
  16. branches ;
  17. next     1.6;
  18.  
  19. 1.6
  20. date     90.06.28.15.14.34;  author rab;  state Exp;
  21. branches ;
  22. next     1.5;
  23.  
  24. 1.5
  25. date     90.02.16.16.12.46;  author shirriff;  state Exp;
  26. branches ;
  27. next     1.4;
  28.  
  29. 1.4
  30. date     89.08.15.12.28.44;  author rab;  state Exp;
  31. branches ;
  32. next     1.3;
  33.  
  34. 1.3
  35. date     89.06.16.09.34.40;  author jhh;  state Exp;
  36. branches ;
  37. next     1.2;
  38.  
  39. 1.2
  40. date     88.10.31.12.35.46;  author brent;  state Exp;
  41. branches ;
  42. next     1.1;
  43.  
  44. 1.1
  45. date     88.10.31.11.56.02;  author brent;  state Exp;
  46. branches ;
  47. next     ;
  48.  
  49.  
  50. desc
  51. @Program to install a boot program on a disk header
  52. @
  53.  
  54.  
  55. 1.8
  56. log
  57. @didn't start boot program at correct sector for decstations.
  58. @
  59. text
  60. @/* 
  61.  * installboot.c --
  62.  *
  63.  *    Copy a boot program to the correct place on the disk.
  64.  *
  65.  * Copyright 1986 Regents of the University of California
  66.  * All rights reserved.
  67.  * Permission to use, copy, modify, and distribute this
  68.  * software and its documentation for any purpose and without
  69.  * fee is hereby granted, provided that the above copyright
  70.  * notice appear in all copies.  The University of California
  71.  * makes no representations about the suitability of this
  72.  * software for any purpose.  It is provided "as is" without
  73.  * express or implied warranty.
  74.  */
  75.  
  76. #ifndef lint
  77. static char rcsid[] = "$Header: /sprite/src/admin/installboot.dlong/RCS/installboot.c,v 1.7 91/01/04 23:41:40 dlong Exp $ SPRITE (Berkeley)";
  78. #endif
  79.  
  80. #include <sprite.h>
  81. #include <option.h>
  82. #include <disk.h>
  83.  
  84. #include <stdio.h>
  85. #include <errno.h>
  86. #include <sys/file.h>
  87.  
  88. /*
  89.  * Settable via the command line.
  90.  */
  91. Boolean unixStyleBootFile = FALSE;
  92. Boolean keepHeader = FALSE;
  93.  
  94. /*
  95.  * The following are used to go from a command line like
  96.  * bootInstall -dev rsd0
  97.  * to /dev/rsd0a     - for the partition that has the disk label
  98.  * and to /dev/rsd0b    - for the partition to format.
  99.  */
  100. char *deviceName;        /* Set to "rsd0" or "rxy1", etc. */
  101. char defaultFirstPartName[] = "a";
  102. char *firstPartName = defaultFirstPartName;
  103. char devDirectory[] = "/dev/";
  104.  
  105. Option optionArray[] = {
  106.     {OPT_STRING, "dev", (Address)&deviceName,
  107.     "Required: Name of device, eg \"rsd0\" or \"rxy1\""},
  108.     {OPT_STRING, "part", (Address)&firstPartName,
  109.     "Optional: Partition ID: (a, b, c, d, e, f, g)"},
  110.     {OPT_TRUE, "noStrip", (Address)&keepHeader,
  111.     "Do not strip off the a.out header (sun4c)\n",},
  112.     {OPT_TRUE, "u", (Address)&unixStyleBootFile,
  113.     "The boot file has no a.out header (unix style)\n",},
  114. };
  115. int numOptions = sizeof(optionArray) / sizeof(Option);
  116.  
  117. /*
  118.  * Forward Declarations.
  119.  */
  120. ReturnStatus InstallBoot();
  121. ReturnStatus DecHeader(), SunHeader();
  122.  
  123.  
  124. /*
  125.  *----------------------------------------------------------------------
  126.  *
  127.  * main --
  128.  *
  129.  *    Create the required file names from the command line
  130.  *    arguments.  Then open the first partition on the disk
  131.  *    and copy the boot program there.
  132.  *
  133.  * Results:
  134.  *    None.
  135.  *
  136.  * Side effects:
  137.  *    Calls InstallBoot
  138.  *
  139.  *----------------------------------------------------------------------
  140.  */
  141. main(argc, argv)
  142.     int argc;
  143.     char *argv[];
  144. {
  145.     ReturnStatus status;    /* status of system calls */
  146.     int diskFID;        /* File ID for first parition on the disk */
  147.     int bootFID;        /* File ID for partiton to format */
  148.     char firstPartitionName[64];
  149.     char *bootFile;
  150.  
  151.     argc = Opt_Parse(argc, argv, optionArray, numOptions);
  152.  
  153.     if (deviceName == (char *)0) {
  154.     printf("Specify device name with -dev option\n");
  155.     status = FAILURE;
  156.     } else if (argc < 2) {
  157.     printf("Specify boot program after options\n");
  158.     status = FAILURE;
  159.     } else {
  160.     bootFile = argv[1];
  161.     status = SUCCESS;
  162.     }
  163.     if (status != SUCCESS) {
  164.     exit(FAILURE);
  165.     }
  166.     /*
  167.      * Gen up the name of the first partition on the disk.
  168.      */
  169.     (void)strcpy(firstPartitionName, devDirectory);    /* eg. /dev/ */
  170.     (void)strcat(firstPartitionName, deviceName);    /* eg. /dev/rxy0 */
  171.     (void)strcat(firstPartitionName, firstPartName);    /* eg. /dev/rxy0a */
  172.  
  173.     diskFID = open(firstPartitionName, O_RDWR, 0);
  174.     if (diskFID < 0) {
  175.     fprintf(stderr, "Can't open \"%s\": %s\n",
  176.                   firstPartitionName, strerror(errno));
  177.     exit(status);
  178.     }
  179.     bootFID = open(bootFile, O_RDONLY, 0);
  180.     if (bootFID < 0) {
  181.     fprintf(stderr, "Can't open boot file \"%s\": %s\n",
  182.                   bootFile, strerror(errno));
  183.     exit(status);
  184.     }
  185.     status = InstallBoot(diskFID, bootFID);
  186.  
  187.     exit(status);
  188. }
  189.  
  190. /*
  191.  *----------------------------------------------------------------------
  192.  *
  193.  * InstallBoot --
  194.  *
  195.  *    Write a boot program to the boot sectors of the disk.
  196.  *
  197.  * Results:
  198.  *    An error code.
  199.  *
  200.  * Side effects:
  201.  *    Write all over the disk partition.
  202.  *
  203.  *----------------------------------------------------------------------
  204.  */
  205. ReturnStatus
  206. InstallBoot(diskFID, bootFID)
  207.     int diskFID;    /* Handle on the first partition of the disk */
  208.     int bootFID;    /* Handle on the boot program */
  209. {
  210.     ReturnStatus status;
  211.     register int numBlocks;
  212. #if 1    
  213.     Disk_Label *diskInfoPtr;
  214. #else    
  215.     Disk_Info  *diskInfoPtr;
  216. #endif    
  217.     Dec_DiskBoot    decBootInfo;
  218.     int bytesRead;
  219.     Address sector;
  220.     int sectorIndex;
  221.     Address loadAddr;
  222.     Address execAddr;
  223.     int    length;
  224.     int    headerSize;
  225.     int toRead;
  226.     int decDisk = 0;    /* 1 if this is a Dec disk. */
  227.  
  228.     /*
  229.      * Read the copy of the super block at the beginning of the partition
  230.      * to find out basic disk geometry and where to write the boot program.
  231.      */
  232. #if 1
  233.     if ((diskInfoPtr = Disk_ReadLabel(diskFID)) == NULL) {
  234.     return FAILURE;
  235.     }
  236. #else    
  237.     diskInfoPtr = Disk_ReadDiskInfo(diskFID, 0);
  238.     if (diskInfoPtr == (Disk_Info *)0) {
  239.     return(FAILURE);
  240.     }
  241. #endif    
  242.     if (Disk_ReadDecLabel(diskFID) != (Dec_DiskLabel *)0) {
  243.     decDisk++;
  244.     }
  245.  
  246.     if ((headerSize = SunHeader(bootFID, &loadAddr, &execAddr, &length)) < 0) {
  247.     if (DecHeader(bootFID, &loadAddr, &execAddr, &length) != SUCCESS) {
  248.         printf("Need impure text format (OMAGIC) file\n");
  249.         return FAILURE;
  250.     }
  251.     }
  252.  
  253.     if (keepHeader) {
  254.     lseek(bootFID, 0L, 0);
  255.     length += headerSize;
  256.     }
  257.  
  258.     /*
  259.      * Write the boot information block.
  260.      */
  261.     if (decDisk) {
  262.     diskInfoPtr->bootSector = DEC_BOOT_SECTOR + 1;
  263.     decBootInfo.magic = DEC_BOOT_MAGIC;
  264.     decBootInfo.mode = 0;
  265.     decBootInfo.loadAddr = (int) loadAddr;
  266.     decBootInfo.execAddr = (int) execAddr;
  267.     decBootInfo.map[0].numBlocks = diskInfoPtr->numBootSectors;
  268.     decBootInfo.map[0].startBlock = diskInfoPtr->bootSector;
  269.     decBootInfo.map[1].numBlocks = 0;
  270.     status = Disk_SectorWrite(diskFID, DEC_BOOT_SECTOR, 1, &decBootInfo);
  271.     if (status < 0) {
  272.         fprintf(stderr, "Sector write %d failed: ", DEC_BOOT_SECTOR);
  273.         perror("");
  274.         return(errno);
  275.     }
  276.     }
  277.     /*
  278.      * Write the remaining code to the correct place on the disk.
  279.      */
  280.     sector = (Address)malloc(DEV_BYTES_PER_SECTOR);
  281.     for (sectorIndex=0 ; sectorIndex < diskInfoPtr->numBootSectors && length>0;
  282.              sectorIndex++) {
  283.     bzero(sector, DEV_BYTES_PER_SECTOR);
  284.     toRead = length < DEV_BYTES_PER_SECTOR ? length :
  285.         DEV_BYTES_PER_SECTOR;
  286.     bytesRead = read(bootFID, sector, toRead);
  287.     if (bytesRead < toRead) {
  288.         perror("Boot file read failed");
  289.         return(status);
  290.     }
  291.     if (bytesRead > 0) {
  292.         length -= bytesRead;
  293.         status = Disk_SectorWrite(diskFID,
  294.                  diskInfoPtr->bootSector + sectorIndex,
  295.                  1, sector);
  296.         if (status < 0) {
  297.         fprintf(stderr, "Sector write %d failed: ", sectorIndex);
  298.         perror("");
  299.         return(errno);
  300.         }
  301.     } else {
  302.         sectorIndex++;
  303.         break;
  304.     }
  305.     }
  306.     printf("Wrote %d sectors\n", sectorIndex);
  307.     if (length > 0) {
  308.     printf("Warning: didn't reach end of boot program!\n");
  309.     }
  310.     return(SUCCESS);
  311. }
  312. @
  313.  
  314.  
  315. 1.7
  316. log
  317. @checking this in for dlong -- jhh
  318. @
  319. text
  320. @d203 1
  321. @
  322.  
  323.  
  324. 1.6
  325. log
  326. @Converted to use the new disk.h.
  327. @
  328. text
  329. @d18 1
  330. a18 1
  331. static char rcsid[] = "$Header: /sprite/src/admin/installboot/RCS/installboot.c,v 1.5 90/02/16 16:12:46 shirriff Exp Locker: rab $ SPRITE (Berkeley)";
  332. d33 1
  333. d37 1
  334. a37 1
  335.  * bootInstall -D rsd0
  336. d47 1
  337. a47 1
  338.     {OPT_STRING, "D", (Address)&deviceName,
  339. d49 4
  340. d95 1
  341. a95 1
  342.     printf("Specify device name with -D option\n");
  343. a158 1
  344.     Fsdm_DomainHeader *headerPtr;
  345. d165 1
  346. d187 1
  347. a187 1
  348.     if (SunHeader(bootFID, &loadAddr, &execAddr, &length) != SUCCESS) {
  349. d192 5
  350. @
  351.  
  352.  
  353. 1.5
  354. log
  355. @Modified so coff format boot files can be used.  Made a bunch of
  356. small changes.  Changed to work for Dec boot program.
  357. @
  358. text
  359. @d18 1
  360. a18 1
  361. static char rcsid[] = "$Header: /sprite/src/admin/installboot/RCS/installboot.c,v 1.4 89/08/15 12:28:44 rab Exp Locker: brent $ SPRITE (Berkeley)";
  362. d23 1
  363. a23 1
  364. #include <diskUtils.h>
  365. d148 5
  366. a152 1
  367.     Disk_Info *diskInfoPtr;
  368. d168 5
  369. d177 1
  370. d188 1
  371. @
  372.  
  373.  
  374. 1.4
  375. log
  376. @Replaced <procAOUT.h> with <procMach.h> and changed
  377. references to proc_AOUT with procExecHeader.
  378. @
  379. text
  380. @d18 1
  381. a18 1
  382. static char rcsid[] = "$Header: /sprite/src/admin/installboot/RCS/installboot.c,v 1.3 89/06/16 09:34:40 jhh Exp Locker: rab $ SPRITE (Berkeley)";
  383. a23 1
  384. #include <kernel/procMach.h>
  385. d57 1
  386. d149 2
  387. a150 2
  388.     FsDomainHeader *headerPtr;
  389.     ProcExecHeader aout;
  390. d154 5
  391. d168 3
  392. d172 6
  393. d179 1
  394. a179 2
  395.      * Read the program header to find out how big it is.  We have to
  396.      * trim this header before writing the boot program to disk.
  397. d181 14
  398. a194 4
  399.     bytesRead = read(bootFID, (char *)&aout, sizeof(ProcExecHeader));
  400.     if (bytesRead < 0) {
  401.     perror("Can't read a.out header");
  402.     return(errno);
  403. a195 5
  404.     if (aout.magic != PROC_OMAGIC) {
  405.     printf("Magic is <0%o>, need impure text format <0%o>\n", aout.magic,
  406.          PROC_OMAGIC);
  407.     return(FAILURE);
  408.     }
  409. d197 1
  410. a197 2
  411.      * Now that the header is skipped just write the remaining code
  412.      * to the correct place on the disk.
  413. d200 1
  414. a200 1
  415.     for (sectorIndex=0 ; sectorIndex < diskInfoPtr->numBootSectors;
  416. d203 4
  417. a206 2
  418.     bytesRead = read(bootFID, sector, DEV_BYTES_PER_SECTOR);
  419.     if (bytesRead < 0) {
  420. d211 1
  421. d226 3
  422. @
  423.  
  424.  
  425. 1.3
  426. log
  427. @Converted to use new c library correctly.
  428. @
  429. text
  430. @d18 2
  431. a19 2
  432. static char rcsid[] = "$Header: /sprite/src/admin/installboot/RCS/installboot.c,v 1.2 88/10/31 12:35:46 brent Exp $ SPRITE (Berkeley)";
  433. #endif not lint
  434. d24 1
  435. a24 1
  436. #include <kernel/procAOUT.h>
  437. d150 1
  438. a150 1
  439.     Proc_AOUT aout;
  440. d168 1
  441. a168 1
  442.     bytesRead = read(bootFID, (char *)&aout, sizeof(Proc_AOUT));
  443. @
  444.  
  445.  
  446. 1.2
  447. log
  448. @Converted to new C library
  449. @
  450. text
  451. @d18 1
  452. a18 1
  453. static char rcsid[] = "$Header: bootInstall.c,v 1.1 88/06/02 13:13:19 brent Exp $ SPRITE (Berkeley)";
  454. d21 4
  455. a24 4
  456. #include "sprite.h"
  457. #include "option.h"
  458. #include "diskUtils.h"
  459. #include "kernel/procAOUT.h"
  460. d26 3
  461. a28 2
  462. #include "stdio.h"
  463. #include "errno.h"
  464. d109 1
  465. a109 1
  466.     diskFID = open(firstPartitionName, FS_READ|FS_WRITE, 0);
  467. d115 1
  468. a115 1
  469.     bootFID = open(bootFile, FS_READ, 0);
  470. d173 3
  471. a175 5
  472.     if (PROC_BAD_MAGIC_NUMBER(aout)) {
  473.     printf("Bad magic number on boot file <0%o>\n", aout.magic);
  474.     return(FAILURE);
  475.     } else if (aout.magic != PROC_OMAGIC) {
  476.     printf("Need impure text format, magic <0%o>\n", PROC_OMAGIC);
  477. @
  478.  
  479.  
  480. 1.1
  481. log
  482. @Initial revision
  483. @
  484. text
  485. @d2 1
  486. a2 1
  487.  * bootInstall.c --
  488. d8 7
  489. a22 1
  490. #include "io.h"
  491. d26 3
  492. d46 1
  493. a46 1
  494.     {OPT_STRING, 'D', (Address)&deviceName,
  495. d48 1
  496. a48 1
  497.     {OPT_TRUE, 'u', (Address)&unixStyleBootFile,
  498. d56 1
  499. a56 1
  500. ReturnStatus BootInstall();
  501. d72 1
  502. a72 1
  503.  *    Calls BootInstall
  504. d86 1
  505. a86 1
  506.     (void)Opt_Parse(&argc, argv, numOptions, optionArray);
  507. d89 1
  508. a89 1
  509.     Io_Print("Specify device name with -D option\n");
  510. d92 1
  511. a92 1
  512.     Io_Print("Specify boot program after options\n");
  513. d99 1
  514. a99 1
  515.     Proc_Exit(FAILURE);
  516. d104 3
  517. a106 3
  518.     String_Copy(devDirectory, firstPartitionName);    /* eg. /dev/ */
  519.     String_Cat(deviceName, firstPartitionName);        /* eg. /dev/rxy0 */
  520.     String_Cat(firstPartName, firstPartitionName);    /* eg. /dev/rxy0a */
  521. d108 5
  522. a112 5
  523.     status = Fs_Open(firstPartitionName, FS_READ|FS_WRITE, 0, &diskFID);
  524.     if (status != SUCCESS) {
  525.     Io_PrintStream(io_StdErr, "Can't open \"%s\" <%x>\n",
  526.                   firstPartitionName, status);
  527.     Proc_Exit(status);
  528. d114 5
  529. a118 5
  530.     status = Fs_Open(bootFile, FS_READ, 0, &bootFID);
  531.     if (status != SUCCESS) {
  532.     Io_PrintStream(io_StdErr, "Can't open boot file \"%s\" <%x>\n",
  533.                   bootFile, status);
  534.     Proc_Exit(status);
  535. d120 1
  536. a120 3
  537.     Io_PrintStream(io_StdErr, "Opened boot file <%x>\n", status);
  538.     Io_Flush(io_StdErr);
  539.     status = BootInstall(diskFID, bootFID);
  540. d122 1
  541. a122 5
  542.     Io_Flush(io_StdErr);
  543.     Io_Flush(io_StdOut);
  544.     (void)Fs_Close(diskFID);
  545.     (void)Fs_Close(bootFID);
  546.     Proc_Exit(status);
  547. d128 1
  548. a128 1
  549.  * BootInstall --
  550. d141 1
  551. a141 1
  552. BootInstall(diskFID, bootFID)
  553. d147 1
  554. a147 1
  555.     BasicDiskInfo *diskInfoPtr;
  556. d158 2
  557. a159 2
  558.     diskInfoPtr = ReadBasicDiskInfo(diskFID, 0);
  559.     if (diskInfoPtr == (BasicDiskInfo *)0) {
  560. d167 4
  561. a170 3
  562.     status = Fs_Read(bootFID, sizeof(Proc_AOUT), &aout, &bytesRead);
  563.     if (status != SUCCESS) {
  564.     return(status);
  565. d173 1
  566. a173 1
  567.     Io_Print("Bad magic number on boot file <0%o>\n", aout.magic);
  568. d176 1
  569. a176 1
  570.     Io_Print("Need impure text format, magic <0%o>\n", PROC_OMAGIC);
  571. d183 1
  572. a183 1
  573.     sector = (Address)Mem_Alloc(DEV_BYTES_PER_SECTOR);
  574. d186 4
  575. a189 4
  576.     Byte_Zero(DEV_BYTES_PER_SECTOR, sector);
  577.     status = Fs_Read(bootFID, DEV_BYTES_PER_SECTOR, sector, &bytesRead);
  578.     if (status != SUCCESS) {
  579.         Stat_PrintMsg(status, "Boot file read failed");
  580. d193 1
  581. a193 1
  582.         status = SectorWrite(diskFID,
  583. d196 4
  584. a199 5
  585.         if (status != SUCCESS) {
  586.         Io_PrintStream(io_StdErr, "Sector write %d failed: ",
  587.                       sectorIndex);
  588.         Stat_PrintMsg(status, "");
  589.         return(status);
  590. d206 1
  591. a206 1
  592.     Io_Print("Wrote %d sectors\n", sectorIndex);
  593. @
  594.